/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */


using System.Runtime.InteropServices;
using int64_t = long;
using int32_t = int;

namespace Dora
{
	internal static partial class Native
	{
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int32_t rendertarget_type();
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int32_t rendertarget_get_width(int64_t self);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int32_t rendertarget_get_height(int64_t self);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_set_camera(int64_t self, int64_t val);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_set_camera_null(int64_t self);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int64_t rendertarget_get_camera(int64_t self);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int64_t rendertarget_get_texture(int64_t self);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_render(int64_t self, int64_t target);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_render_clear(int64_t self, int32_t color, float depth, int32_t stencil);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_render_clear_with_target(int64_t self, int64_t target, int32_t color, float depth, int32_t stencil);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern void rendertarget_save_async(int64_t self, int64_t filename, int32_t func0, int64_t stack0);
		[DllImport(Dll, CallingConvention = CallingConvention.Cdecl)]
		public static extern int64_t rendertarget_new(int32_t width, int32_t height);
	}
} // namespace Dora

namespace Dora
{
	/// <summary>
	/// A RenderTarget is a buffer that allows you to render a Node into a texture.
	/// </summary>
	public partial class RenderTarget : Object
	{
		public static new (int typeId, CreateFunc func) GetTypeInfo()
		{
			return (Native.rendertarget_type(), From);
		}
		protected RenderTarget(long raw) : base(raw) { }
		internal static new RenderTarget From(long raw)
		{
			return new RenderTarget(raw);
		}
		internal static new RenderTarget? FromOpt(long raw)
		{
			return raw == 0 ? null : new RenderTarget(raw);
		}
		/// <summary>
		/// The width of the rendering target.
		/// </summary>
		public int Width
		{
			get => Native.rendertarget_get_width(Raw);
		}
		/// <summary>
		/// The height of the rendering target.
		/// </summary>
		public int Height
		{
			get => Native.rendertarget_get_height(Raw);
		}
		/// <summary>
		/// The camera used for rendering the scene.
		/// </summary>
		public Camera? Camera
		{
			set
			{
				if (value == null) Native.rendertarget_set_camera_null(Raw);
				else Native.rendertarget_set_camera(Raw, value.Raw);
			}
			get => Camera.FromOpt(Native.rendertarget_get_camera(Raw));
		}
		/// <summary>
		/// The texture generated by the rendering target.
		/// </summary>
		public Texture2D Texture
		{
			get => Texture2D.From(Native.rendertarget_get_texture(Raw));
		}
		/// <summary>
		/// Renders a node to the target without replacing its previous contents.
		/// </summary>
		/// <param name="target">The node to be rendered onto the render target.</param>
		public void Render(Node target)
		{
			Native.rendertarget_render(Raw, target.Raw);
		}
		/// <summary>
		/// Clears the previous color, depth and stencil values on the render target.
		/// </summary>
		/// <param name="color">The clear color used to clear the render target.</param>
		/// <param name="depth">Optional. The value used to clear the depth buffer of the render target. Default is 1.</param>
		/// <param name="stencil">Optional. The value used to clear the stencil buffer of the render target. Default is 0.</param>
		public void RenderClear(Color color, float depth = 1.0f, int stencil = 0)
		{
			Native.rendertarget_render_clear(Raw, (int)color.ToARGB(), depth, stencil);
		}
		/// <summary>
		/// Renders a node to the target after clearing the previous color, depth and stencil values on it.
		/// </summary>
		/// <param name="target">The node to be rendered onto the render target.</param>
		/// <param name="color">The clear color used to clear the render target.</param>
		/// <param name="depth">The value used to clear the depth buffer of the render target. Default can be 1.</param>
		/// <param name="stencil">The value used to clear the stencil buffer of the render target. Default can be 0.</param>
		public void RenderClear(Node target, Color color, float depth = 1.0f, int stencil = 0)
		{
			Native.rendertarget_render_clear_with_target(Raw, target.Raw, (int)color.ToARGB(), depth, stencil);
		}
		/// <summary>
		/// Saves the contents of the render target to a PNG file asynchronously.
		/// </summary>
		/// <param name="filename">The name of the file to save the contents to.</param>
		/// <param name="handler">The function to call when the save operation is complete. The function will be passed a boolean value indicating whether the save operation was successful.</param>
		public void SaveAsync(string filename, System.Action<bool> handler)
		{
			var stack0 = new CallStack();
			var stack_raw0 = stack0.Raw;
			var func_id0 = Bridge.PushFunction(() =>
			{
				handler(stack0.PopBool());
			});
			Native.rendertarget_save_async(Raw, Bridge.FromString(filename), func_id0, stack_raw0);
		}
		public RenderTarget(int width, int height) : this(Native.rendertarget_new(width, height)) { }
	}
} // namespace Dora
